package com.atguigu.gmall.list.listerner;

import com.atguigu.gmall.list.service.ListSerivce;
import com.atguigu.gmall.rabbit.constant.MqConst;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

@Component
public class ConsumerUpdateEs {

    @Resource
    private ListSerivce listSerivce;

    //上架商品  添加或修改ES
    @RabbitListener(bindings = @QueueBinding(
            //绑定队列
            value = @Queue(value = MqConst.QUEUE_GOODS_UPPER,durable = "true",autoDelete = "false"),
            //绑定交换机
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_GOODS),
            key = MqConst.ROUTING_GOODS_UPPER
    ))
    public void upperGoods(Long skuId, Message message, Channel channel){

        //应答
        try {
            listSerivce.upperGoods(skuId);
            //basicAck：基本ack应答             获取消息属性            获取标签            不批量处理
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {//注：为捕获service中方法可能出现的异常
            //basicNack：不应答
            if (!message.getMessageProperties().isRedelivered()) {//message中属性是否为重新发送
                try {
                    //是否批量处理        是否放回队列
                    channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            //basicReject：拒绝应答
            else {
                try {
                    //第二次发送     拒绝      是否放回队列
                    channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

    //下架商品  删除ES
    @RabbitListener(bindings = @QueueBinding(
            //绑定队列
            value = @Queue(value = MqConst.QUEUE_GOODS_LOWER,durable = "true",autoDelete = "false"),
            //绑定交换机
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_GOODS),
            key = MqConst.ROUTING_GOODS_LOWER
    ))
    public void lowerGoods(Long skuId,Message message,Channel channel){
        try {
            listSerivce.lowerGoods(skuId);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            if (!message.getMessageProperties().isRedelivered()) {
                try {
                    channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }else {
                try {
                    channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

}
